<?php

namespace App\Console\Commands;

use App\Models\Family;
use App\Models\Hospital;
use App\Models\HospitalAppointIntervalNum;
use App\Models\FamilyType;
use App\Models\old\Appoint;
use App\Models\old\HospitalAppoint;
use App\Models\old\HospitalAppointStreet;
use App\Models\old\HospitalAppointVaccineTimeNum;
use App\Models\old\HospitalAppointWeek;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Street;
use App\Models\TempUserid;
use Illuminate\Console\Command;

class migrationAppoints extends Command
{
    public static $cycleNum = [1 => 7, 2 => 14, 3 => 30,4=>1,5=>2,6=>3,7=>4,8=>5,9=>60,10=>90,11=>120];

    public static $rtText = [
        0 => '00:00',
        5 => '05:00',
        6 => '06:00',
        7 => '07:00',
        11 => '11:00',
        12 => '12:00',
        13 => '13:00',
        14 => '14:00',
        15 => '15:00',
        16 => '16:00',
        17 => '17:00',
        18 => '18:00',
        19 => '19:00',
        20 => '20:00',
        21 => '21:00',
        22 => '22:00',
        23 => '23:00',
    ];
    public static $timeText = [
        1 => [
            '1' => '08:00-09:00',
            '2' => '09:00-10:00',
            '3' => '10:00-11:00',
            '4' => '13:00-14:00',
            '5' => '14:00-15:00',
            '6' => '15:00-15:30',
        ],
        2 => [
            '7' => '08:00-08:30',
            '8' => '08:30-09:00',
            '9' => '09:00-09:30',
            '10' => '09:30-10:00',
            '11' => '10:00-10:30',
            '12' => '10:30-11:00',
            '19' => '11:00-11:30',
            '20' => '11:30-12:00',
            '13' => '13:00-13:30',
            '14' => '13:30-14:00',
            '15' => '14:00-14:30',
            '16' => '14:30-15:00',
            '17' => '15:00-15:30',
            '18' => '15:30-16:00'
        ],
    ];
    public static $timeText1 = [
            '1' => '08:00-09:00',
            '2' => '09:00-10:00',
            '3' => '10:00-11:00',
            '4' => '13:00-14:00',
            '5' => '14:00-15:00',
            '6' => '15:00-15:30',
            '7' => '08:00-08:30',
            '8' => '08:30-09:00',
            '9' => '09:00-09:30',
            '10' => '09:30-10:00',
            '11' => '10:00-10:30',
            '12' => '10:30-11:00',
            '19' => '11:00-11:30',
            '20' => '11:30-12:00',
            '13' => '13:00-13:30',
            '14' => '13:30-14:00',
            '15' => '14:00-14:30',
            '16' => '14:30-15:00',
            '17' => '15:00-15:30',
            '18' => '15:30-16:00'
    ];
    public static $typeText = [
        1 => 1,
        2 => 2,
        8 => 3,
        4 => 13,
        5 => 4,
        6 => 5,
        3 => 6,
        7 => 7,
        9 => 8,
        10 => 9,
        11 => 10,
        12 => 11,
        13 => false,
    ];

    protected static $month = [
        1=>[
            ['month'=>1,'month_name'=>'满月','is_check'=>0],
        ],
        2=>[
            ['month'=>2,'month_name'=>'2个月','is_check'=>0],
            ['month'=>3,'month_name'=>'3个月','is_check'=>0],
        ],
        3=>[
            ['month'=>5,'month_name'=>'5个月','is_check'=>0],
            ['month'=>6,'month_name'=>'6个月','is_check'=>0],

        ],
        4=>[
            ['month'=>8,'month_name'=>'8个月','is_check'=>0],
            ['month'=>9,'month_name'=>'9个月','is_check'=>0],

        ],
        5=>[
            ['month'=>11,'month_name'=>'11个月','is_check'=>0],
            ['month'=>12,'month_name'=>'12个月','is_check'=>0],

        ],
        6=>[
            ['month'=>17,'month_name'=>'1岁5个月','is_check'=>0],
            ['month'=>18,'month_name'=>'1岁6个月','is_check'=>0],
        ],
        7=>[
            ['month'=>23,'month_name'=>'1岁11个月','is_check'=>0],
            ['month'=>24,'month_name'=>'1岁11个月','is_check'=>0],
        ],
        8=>[
            ['month'=>29,'month_name'=>'2岁5个月','is_check'=>0],
            ['month'=>30,'month_name'=>'2岁6个月','is_check'=>0],
        ],
        9=>[
            ['month'=>35,'month_name'=>'2岁11个月','is_check'=>0],
            ['month'=>36,'month_name'=>'2岁12个月','is_check'=>0],
            ['month'=>37,'month_name'=>'3岁1个月','is_check'=>0],
            ['month'=>38,'month_name'=>'3岁2个月','is_check'=>0],
            ['month'=>39,'month_name'=>'3岁3个月','is_check'=>0],
            ['month'=>40,'month_name'=>'3岁4个月','is_check'=>0],
            ['month'=>41,'month_name'=>'3岁5个月','is_check'=>0],
            ['month'=>42,'month_name'=>'3岁6个月','is_check'=>0],
            ['month'=>43,'month_name'=>'3岁7个月','is_check'=>0],
            ['month'=>44,'month_name'=>'3岁8个月','is_check'=>0],
            ['month'=>45,'month_name'=>'3岁9个月','is_check'=>0],
            ['month'=>46,'month_name'=>'3岁10个月','is_check'=>0],
            ['month'=>47,'month_name'=>'3岁11个月','is_check'=>0],
            ['month'=>48,'month_name'=>'3岁12个月','is_check'=>0],
        ],
    ];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:appoints';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        ini_set('memory_limit', '4800M');

        //$userids = TempUserid::query()->pluck('userid')->toArray();
        $appoints = Appoint::query()
            ->whereIn('doctorid',['184793','176156'])
            ->where('state',1)
            ->orderBy('id','desc')
            ->get();
        var_dump($appoints->count());
        foreach($appoints as $v){
            var_dump($v->id);
            $appoint = Appoint::query()->where('id',$v->id)->first();
            $row['user_id'] = $appoint->userid;
            $family = Family::query()->where('old_id',$appoint->childid)->first();
            if(!$family){
                $family_id = 0;
                $row['old_family_id'] = $appoint->childid;
            }else{
                $family_id = $family->id;
            }
            $row['family_id'] = $family_id;
            $row['appoint_date'] = date('Y-m-d',$appoint->appoint_date);
            $row['type'] = self::$typeText[$appoint->type];
            $hospitalAppoint = \App\Models\HospitalAppoint::query()
                ->where('hospital_id',$appoint->doctorid)
                ->where('type',$row['type'])
                ->first();


            if(!$hospitalAppoint) {
                var_dump($appoint->doctorid);
                var_dump($row['type']);
                continue;
            }

            $ordInterval = self::$timeText1[$appoint->appoint_time];
            $time = explode('-',$ordInterval);
            list($start,$end) = $time;
            $interval = HospitalAppointInterval::query()->where('hospital_appoint_id',$hospitalAppoint->id)
                ->where('start',$start)
                ->where('end',$end)
                ->first();


            $row['appoint_time'] = $interval->id;
            $row['status'] = $appoint->state;
            $row['hospital_id'] = $appoint->doctorid;
            if(in_array($row['type'],[2,13])){
                $row['project'] = $appoint->vaccine;
            }elseif($row['type']==1){
                $row['project'] = $appoint->month;
            }else{
                $row['project'] = 0;

            }
            $row['source'] = $appoint->mode == 0?1:2;
            $row['created_at'] = date('Y-m-d H:i:s',$appoint->createtime);
            $row['id'] = $appoint->id;
            \App\Models\Appoint::query()->updateOrCreate(['id'=>$appoint->id],$row);


        }





        return 0;
    }
}
